package linker;

import java.io.FileNotFoundException;
import java.io.FileReader;

import javax.xml.stream.*;
import javax.xml.stream.events.XMLEvent;

import dto.SalidaDTO;

import util.CONFIG;
import util.PARAMS;

public class CONFIGSQL {
	
	private String user = "test", 
			pass = "test", 
			server = "test", 
			database = "test";
	private int port = 0;
	
	private CONFIGSQL() {
		String path = CONFIG.class.getProtectionDomain().getCodeSource().getLocation().getPath();
		path = path.replace(".jar", "-SQL.xml");
		//System.out.println(path);
		//path += "LogParser-SQL.xml"; //REMOVER
		XMLInputFactory factory = XMLInputFactory.newInstance();
		XMLEventReader eventReader = null;
		try {
			eventReader = factory.createXMLEventReader(new FileReader(path));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println(path); //REMOVER
			e.printStackTrace();
		} catch (XMLStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		while(eventReader.hasNext()) {
			try {
				XMLEvent event = eventReader.nextEvent();

				if ((event.isStartElement()) && (event.asStartElement().getName().getLocalPart().equals(PARAMS.XMLSQLTitle))) {
					while (eventReader.hasNext()) {
						XMLEvent event2 = eventReader.nextEvent();
						if ((event2.isStartElement()) && (event2.asStartElement().getName().getLocalPart().equals(PARAMS.XMLSQLAttributes))) {
							while (eventReader.hasNext()) {
								XMLEvent event3 = eventReader.nextEvent();
								if ((event3.isStartElement()) && (event3.asStartElement().getName().getLocalPart().equals(PARAMS.XMLSQLServer))) {
									XMLEvent aux = eventReader.nextEvent();
									server = aux.asCharacters().getData();
									eventReader.nextEvent();
								}
								if ((event3.isStartElement()) && (event3.asStartElement().getName().getLocalPart().equals(PARAMS.XMLSQLPort))) {
									XMLEvent aux = eventReader.nextEvent();
									port = Integer.parseInt(aux.asCharacters().getData());
									eventReader.nextEvent();
								}
								if ((event3.isStartElement()) && (event3.asStartElement().getName().getLocalPart().equals(PARAMS.XMLSQLUser))) {
									XMLEvent aux = eventReader.nextEvent();
									user = aux.asCharacters().getData();
									eventReader.nextEvent();
								}
								if ((event3.isStartElement()) && (event3.asStartElement().getName().getLocalPart().equals(PARAMS.XMLSQLPass))) {
									XMLEvent aux = eventReader.nextEvent();
									pass = aux.asCharacters().getData();
									eventReader.nextEvent();
								}
								if ((event3.isStartElement()) && (event3.asStartElement().getName().getLocalPart().equals(PARAMS.XMLSQLDatabase))) {
									XMLEvent aux = eventReader.nextEvent();
									database = aux.asCharacters().getData();
									eventReader.nextEvent();
								}
								if ((event2.isEndElement()) && (event2.asEndElement().getName().getLocalPart().equals(PARAMS.XMLSQLAttributes)))
									break;
							}
						}
						if ((event.isEndElement()) && (event.asEndElement().getName().getLocalPart().equals(PARAMS.XMLSQLTitle)))
							break;
					}
				}
			} catch (XMLStreamException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private static CONFIGSQL instance = null;
	
	private static CONFIGSQL getInstance() {
		if (instance == null) instance = new CONFIGSQL();
		return instance;
	}
	
	public static String getUser() {
		return getInstance().user;
	}

	public static String getPass() {
		return getInstance().pass;
	}

	public static String getServerName() {
		return getInstance().server;
	}

	public static int getPort() {
		return getInstance().port;
	}

	public static String getDatabaseName() {
		return getInstance().database;
	}

	public static void printParams() {
		System.out.println("user: " + getInstance().user);
		System.out.println("pass: " + getInstance().pass);
		System.out.println("server: " + getInstance().server);
		System.out.println("database: " + getInstance().database);
		System.out.println("port: " + getInstance().port);
	}

	public static String getBCPParams(SalidaDTO dto) {
		String aux = "bcp "
				+ getInstance().database + ".."
				+ dto.tabla + " in "
				+ CONFIG.getPathArchivos()
				+ dto.archivo + " -U"
				+ getInstance().user + " -P"
				+ getInstance().pass + " -c";
		
		return aux;
	}
}
